<template>
  <div class="content">
    <el-tabs v-model="activeName" type="card" addable class="demo-tabs">
      <template #add-icon>
        <el-button icon="Plus" @click="handleTabsAdd" v-auth="'team_class_add'"
          >添加分类</el-button
        >
        <el-button icon="Edit" @click="handleOpen" v-auth="'company/team/add'"
          >添加风采</el-button
        >
      </template>
      <el-tab-pane label="全部分类" name="0" :closable="false"></el-tab-pane>
      <el-tab-pane :name="v.id" v-for="v in tabs" :key="v.id">
        <template #label>
          <span class="title">{{ v.title }}</span>
          <el-icon
            :style="{
              marginLeft: '3px',
            }"
            @click.prevent.stop="handleTabsAdd(v)"
          >
            <Edit
          /></el-icon>
          <el-icon
            :style="{
              marginLeft: '8px',
            }"
            @click.prevent.stop="handleTabsDel(v.id)"
          >
            <Close
          /></el-icon>
        </template>
      </el-tab-pane>
    </el-tabs>
    <el-row align="top" :gutter="20">
      <el-col :span="3">
        <el-card header="选择部门">
          <el-tree
            :data="departmentOption"
            node-key="id"
            default-expand-all
            check-on-click-node
            show-checkbox
            :expand-on-click-node="false"
            check-strictly
            :props="{ label: 'title', value: 'id' }"
            ref="treeRef"
            :highlight-current="true"
            @check-change="handleStaff"
          ></el-tree>
        </el-card>
      </el-col>
      <el-col :span="21">
        <el-card>
          <el-table :data="tableData" row-key="id" height="100%" stripe>
            <el-table-column type="index" label="#" fixed="left" />
            <el-table-column prop="title" label="标题" fixed="left" />
            <el-table-column prop="class_name" label="分类" />
            <el-table-column prop="department_name" sortable label="所属部门" />
            <el-table-column prop="author_name" label="作者" />
            <el-table-column prop="create_time" sortable label="创作时间" />
            <el-table-column prop="update_time" sortable label="最后修改" />
            <el-table-column prop="sort" sortable label="排序" />
            <el-table-column label="操作" fixed="right" width="200">
              <template #default="scope">
                <el-button
                  type="primary"
                  link
                  icon="Edit"
                  @click="handleOpen(scope.row.id)"
                  v-auth="'/company/team/edit'"
                >
                  编辑
                </el-button>
                <el-button
                  type="danger"
                  link
                  icon="DeleteFilled"
                  @click="delInfo(scope.row.id)"
                  v-auth="'/company/team/del'"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            background
            :current-page="curPage"
            :page-size="pageSize"
            layout="total, sizes,prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog
      v-model="classVisible"
      @close="initClassForm"
      :title="classDialogTitle"
      width="300px"
    >
      <el-form v-model="classForm">
        <el-form-item label="名称">
          <el-input
            v-model="classForm.title"
            placeholder="请输入分类名称"
          ></el-input>
        </el-form-item>

        <el-form-item label="排序">
          <el-input-number
            v-model="classForm.sort"
            :min="0"
            :max="100"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="classVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSaveClass">确定</el-button>
      </template>
    </el-dialog>
    <Drawer
      ref="teamDrawer"
      @getTable="getTableInfo"
      :department="departmentOption"
      :classList="tabs"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch } from "vue";
import {
  deleteDepartment,
  getDepartmentOptionsApi,
} from "@/api/company/department";
import Drawer from "./component/dialogForm.vue";
import { ElMessage, ElMessageBox, TabPaneName } from "element-plus";
import {
  editClass,
  addClass,
  getClassApi,
  getListAPi,
  delClass,
} from "@/api/company/team";
const activeName = ref("0");
const tabs = ref<any[]>([]);
const classVisible = ref(false);
const classDialogTitle = ref("添加分类");
const teamDrawer = ref<any>(null);
const classForm = ref({
  id: 0,
  title: "",
  sort: 0,
});
const query = reactive({
  department_id: [],
  class_id: "0",
});
const total = ref(0);
watch(
  query,
  () => {
    getTableInfo();
  },
  { deep: true }
);
watch(activeName, (newVal) => {
  query.class_id = newVal;
});
const curPage = ref(1);
const pageSize = ref(20);
const departmentOption = ref<any[]>([]);
const childDialogForm = ref<any>(null);
const tableData = ref([]);
const staffRef = ref<any>(null);
onMounted(() => {
  getClass();
  getDepartmentOptionsApi().then((res) => {
    departmentOption.value = res.data;
  });
  getTableInfo();
});
const getClass = () => {
  getClassApi().then((res) => {
    tabs.value = res.data;
  });
};
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};
const handleTabsAdd = (e?: any) => {
  if (e.id > 0) {
    classForm.value = e;
    classDialogTitle.value = "编辑分类";
  } else {
    initClassForm();
    classDialogTitle.value = "添加分类";
  }
  classVisible.value = true;
};
const handleTabsDel = (id: number) => {
  ElMessageBox.confirm("确认要删除此分类吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    delClass(id).then((res: any) => {
      getClass();
      if (activeName.value == "" + id) {
        activeName.value = "0";
      }
    });
  });
};
const initClassForm = () => {
  classForm.value = {
    id: 0,
    title: "",
    sort: 0,
  };
};
const handleSaveClass = async () => {
  if (classForm.value.id > 0) {
    await editClass(classForm.value);
  } else {
    await addClass(classForm.value);
  }
  getClass();
  classVisible.value = false;
};
function getTableInfo() {
  getListAPi({
    page: curPage.value,
    page_size: pageSize.value,
    ...query,
  }).then((res) => {
    tableData.value = res.data.data;
    total.value = res.data.total;
  });
}

function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除此信息吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteDepartment(id).then((res: any) => {
        getTableInfo();

        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(id?: number) {
  teamDrawer.value.dialogOpen(id || 0);
}

const handleStaff = (e: any, checked: boolean) => {
  if (checked) {
    query.department_id.push(e.id);
  } else {
    query.department_id = query.department_id.filter(
      (item: any) => item != e.id
    );
  }
};

defineExpose({
  getTableInfo,
});
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
